<?PHP
	require 'includes/master.inc.php';

	if(isset($_GET['act']) && $_GET['act'] == "rss")
	{
		$feed = new RSS();
		$feed->title       = "Sparkle Updates";
		$feed->link        = 'http://' . $_SERVER['SERVER_NAME'] . WEB_ROOT . 'sparkle-logs.php';
		$feed->description = "Sparkle Update Requests";
 		$sparks = $db->query("SELECT * FROM sparkle ORDER BY dt DESC LIMIT 50");
		while($spark = mysql_fetch_array($sparks, MYSQL_ASSOC))
		{
			$item = new RSSItem();
			$item->title = $spark['ip'] . " - " . $spark['model'] . "/" . $spark['ramMB'] . "/" . $spark['cpuFreqMHz'] . " (" . $spark['osVersion'] . ")";
			$item->link  = 'http://' . $_SERVER['SERVER_NAME'] . WEB_ROOT . 'sparkle-logs.php#' . md5($spark['dt']);
			$item->setPubDate($spark['dt']); 
			$item->description = nl2br(var_export($spark, true));
			$feed->addItem($item);
		}
		$feed->serve();
		die();
	}

	$auth->admin();

	$sparks   = $db->getRows("SELECT * FROM sparkle ORDER BY dt DESC LIMIT 250");
	
	$db->query("SELECT COUNT(*) FROM sparkle GROUP BY ip");
	$total_ips = mysql_num_rows($db->result);
	
	$last_version = $db->getValue("SELECT dt FROM versions ORDER BY dt DESC LIMIT 1");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Sparkle Logs | Appcaster</title>
	<?PHP include 'inc/meta.inc.php'; ?>
	<link rel="alternate" type="application/rss+xml" title="Sparkle Update Request Feed" href="<?PHP WEBROOT();?>sparkle-logs.php?act=rss" />
</head>

<body id="home">
	<?PHP include 'inc/head.inc.php'; ?>
	<div id="nav">
		<ul>
			<li><a href='<?PHP WEBROOT();?>index.php'>Applications</a></li>
			<li><a href='<?PHP WEBROOT();?>orders.php'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>feedback.php'>Feedback (<?PHP echo Feedback::newCountTotal();?>)</a></li>
			<li class='selected'><a href='<?PHP WEBROOT();?>stats.php'>Stats</a></li>
			<li><a href='<?PHP WEBROOT();?>options.php'>Options</a></li>
		</ul>
	</div>

	<div id="menutabs">
		<ul id="tabnav">
			<li><a href='<?PHP WEBROOT();?>stats.php'>General</a></li>
			<li class='selected'><a href='<?PHP WEBROOT();?>sparkle-logs.php'>Sparkle Logs</a></li>
		</ul>
	</div>

	<div id="contain">
		<p>Unique Users: <?PHP echo number_format($total_ips); ?></p>
		<table class="grid">
			<tr>
				<th>Date</th>
				<th>App</th>
				<th>Version</th>
				<th>OS</th>
				<th>Model</th>
				<th>RAM</th>
				<th>CPU</th>
				<th>Language</th>
			</tr>
			<?PHP foreach($sparks as $s) : ?>
			<?PHP
				if(($last_version !== false) && (strtotime($s['dt']) <= strtotime($last_version)))
				{
					echo "<tr style='background-color:#f00;color:#fff;'><td colspan='9'>New Release</td></tr>";
					$last_version = false;
				}
			?>
			<tr>
				<td><?PHP echo dater("m/d g:ia", $s['dt']); ?></td>
				<td><?PHP echo $s['appName']; ?></td>
				<td><?PHP echo $s['appVersion'];?></td>
				<td><?PHP echo $s['osVersion']; ?></td>
				<td><?PHP echo $s['model']; ?></td>
				<td><?PHP echo $s['ramMB']; ?></td>
				<td><?PHP echo $s['cpuFreqMHz']; ?></td>
				<td><?PHP echo $s['lang']; ?></td>
			</tr>			
			<?PHP endforeach; ?>
		</table>
	</div>
</body>
</html>